package com.bawei.module_moto_circle.model

import android.content.Context
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import com.bawei.lib_common.appContext
import com.bawei.lib_common.widget.nine_pic_grid.Image
import com.bawei.module_moto_circle.R

/**
 *作者：lxy
 *功能:
 *时间：2025/6/1
 */
data class PostModel(
    val current: Int,
    val pages: Int,
    val records: List<PostInfo>,
    val size: Int,
    val total: Int
)

data class PostInfo(
    val aftersale_result: Any,
    val aftersale_status: Int,
    val aftersale_time: Any,
    val avatar: Avatar?,
    val category_str: String,
    val channel: Int,
    val channel_str: Any,
    var collect_number: Int,
    var comment_number: Int,
    val content: String,
    val create_by: String,
    val create_time: String,
    val delflag: Any,
    val hot_sort: Int,
    val if_aftersale: Int,
    val if_all_sehicle_model: Any,
    val if_all_series: Any,
    val if_audit: Int,
    var if_collect: Int,
    val if_hot: Int,
    var if_like: Int,
    val if_my: Int,
    val if_recommend: Int,
    val if_sensitive: Int,
    val if_tip_off: String,
    val if_transcode: Any,
    val if_up: Int,
    val if_up_str: Any,
    val if_video: Int,
    val image: List<Image>,
    val img_ids: String,
    val lat: Double,
    var like_number: Int,
    val list_show_time: String,
    val location: String,
    val lon: Double,
    val model_name: String,
    val model_nickname: String,
    val posts_id: String,
    val recommend_sort: Int,
    val relation: Int,
    val search_sort: Int,
    val series: String,
    var share_number: Int,
    val tip_off_number: Int,
    val topic_ids: String,
    val topic_str: String,
    val topics: List<Topic>,
    val update_by: String,
    val update_name: String,
    val update_time: String,
    val user_id: String,
    val vehicle_model_id: String,
    val virtual_like_number: Int,
    val virtual_share_number: Int,
    val weight_score: Double,

    // 是否需要展示关注状态，某些特定页面是不展示的
    var needShowFollowState: Boolean? = null
) {
    //是否为官方发布
    open fun isOfficial() = channel == 1

    //显示颜色 官方-显示主题色 其他-显示黑色
    fun requireNameColor(context: Context): Int {
        return if (isOfficial()) {
            ContextCompat.getColor(context, com.bawei.lib_common.R.color.color_d8000f)
        } else {
            ContextCompat.getColor(context, com.bawei.lib_common.R.color.color_101010)
        }
    }

    //显示名称  官方-显示赛科龙官方 其他-显示nikeName
    fun requireName(context: Context): String {
        return if (isOfficial()) {
            context.getString( com.bawei.lib_common.R.string.string_cyclone_official)
        } else {
            model_nickname
        }
    }


    //显示subName  官方-显示赛科龙官方发布 其他-显示modelName
    fun requireSubName(context: Context): String {
        return if (isOfficial()) {
            context.getString(com.bawei.lib_common.R.string.string_cyclone_official_publish)
        } else {
            model_nickname
        }
    }

    //如果为官方发布，需要自己在内容后添加话题
    fun requireContent(): String {
        if (channel == 1 && !topics.isNullOrEmpty()) {
            val sb = StringBuilder()
            sb.append(content)
            topics.forEach {
                sb.append("#${it.topicName}#")
            }
            return sb.toString()
        }
        return content ?: ""
    }


    fun hasFollowed(): Boolean {
        return relation in listOf(
            UserRelationEnum.FOCUS.relation,
            UserRelationEnum.FRIENDS.relation
        )
    }

    fun showFollowState(): Boolean {
        if (needShowFollowState == null)
            needShowFollowState = true

        // 非官方 非自己 非关注列表 才需要显示关注关系
        return channel != 1 && needShowFollowState == true && relation != UserRelationEnum.MYSELF.relation
    }

    fun canFollow(): Boolean {
        return relation in listOf(UserRelationEnum.NONE.relation, UserRelationEnum.FANS.relation)
    }

    fun showAddIcon(): Boolean {
        return relation <= UserRelationEnum.FANS.relation
    }

    fun showMutualIcon(): Boolean {
        return relation == UserRelationEnum.FRIENDS.relation
    }

    fun followStateName(): String {
        return when (relation) {
            0 -> {
                "关注"
            }

            1 -> {
                "关注"
            }

            2 -> {
                "已关注"
            }

            3 -> {
                "互相关注"
            }

            else -> {
                ""
            }
        }
    }

    //格式化点赞数显示
    fun formatLikeNumber(): String {
        val total = like_number + virtual_like_number
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }

    //格式化收藏数显示
    fun formatCollectionNumber(): String {
        val total = collect_number
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }

    //格式化分享数显示
    fun formatShareNumber(): String {
        val total = share_number + virtual_share_number
        return if (total < 10_000) {
            total.toString()
        } else if (total > 10_000) {
            String.format("%sw+", total / 10_000)
        } else {
            "1w"
        }
    }

    fun requireCollectIcon(context: Context): Drawable? {
        return if (if_collect == 1)
            ContextCompat.getDrawable(context, R.mipmap.icon_collection_checked)
        else
            ContextCompat.getDrawable(context, R.mipmap.icon_collection)

    }
    //格式化评论数
    fun formatCommentNumber(): String {
        return if (comment_number < 10_000) {
            comment_number.toString()
        } else if (comment_number > 10_000) {
            String.format("%sw+", comment_number / 10_000)
        } else {
            "1w"
        }
    }

    //是否处于售后状态
    fun inAfterSaleStatus(): Boolean {
        return if_aftersale == 1
    }

    fun requireAfterSaleLabel(): Drawable? {
        if (if_aftersale != 1) return null
        return if (aftersale_status == 2) {
            //已处理
            ContextCompat.getDrawable(appContext, R.mipmap.icon_after_sale_finished_label)
        } else {
            ContextCompat.getDrawable(appContext, R.mipmap.icon_after_sale_label)
        }
    }
}

data class Avatar(
    val contentLength: Int,
    val contentType: String,
    val filename: String,
    val firstImage: Any,
    val id: Int,
    val imageHeight: Int,
    val imageWidth: Int,
    val thumbnailUrl: String,
    val url: String
)

//data class Image(
//    val contentLength: Int,
//    val contentType: String,
//    val filename: String,
//    val firstImage: String,
//    val id: Int,
//    val imageHeight: Int,
//    val imageWidth: Int,
//    val thumbnailUrl: String,
//    val url: String
//)

data class Topic(
    val topicId: Long,
    val topicName: String
)

enum class UserRelationEnum(val relation: Int, val typeName: String) {
    NONE(0, "无关系"), FANS(1, "粉丝"),
    FOCUS(2, "已关注"),
    FRIENDS(3, "互相关注"),
    MYSELF(4, "我自己"),
}